package com.yanqu.road.dao.impl.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionRankDaoImpl extends TempDao {

    public Map<Long, LinkedList<UnionRank>> getCrossUnionRankListMap() {
        String sql = "select * from t_c_union_rank";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, LinkedList<UnionRank>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionRank data = initCrossUnionRank(rs);
                if(!dataMap.containsKey(data.getUnionBaseInfo().getServerId())){
                    dataMap.put(data.getUnionBaseInfo().getServerId(), new LinkedList<>());
                }
                dataMap.get(data.getUnionBaseInfo().getServerId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean updateCrossUnionRankList(long serverId, List<UnionRank> rankList) {
        String sql = "REPLACE INTO t_c_union_rank(serverId,rank,unionUid,unionBaseInfo) VALUES(?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int rank = 1;
            for (UnionRank unionRank : rankList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.BIGINT, serverId));
                para.put(i++, new DbParameter(Types.INTEGER, rank));
                para.put(i++, new DbParameter(Types.VARCHAR, unionRank.getUnionUid()));
                para.put(i++, new DbParameter(Types.NVARCHAR, unionRank.getUnionBaseInfoJsonStr()));
                mapList.add(para);
                rank++;
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    private UnionRank initCrossUnionRank(ResultSet rs) throws SQLException {
        UnionRank data = new UnionRank();
        data.setUnionUid(rs.getString("unionUid"));
        data.setRank(rs.getInt("rank"));
        String value = rs.getString("unionBaseInfo");
        if(null == value){
            value = "";
        }
        UnionBaseInfo unionBaseInfo = JSON.parseObject(value, new TypeReference<UnionBaseInfo>() {});
        if(null != unionBaseInfo){
            data.setUnionBaseInfo(unionBaseInfo);
        }
        return data;
    }
}
